package com.xfxz.merchant.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xfxz.merchant.R;
import com.xfxz.merchant.bean.CouponBean;

import java.util.List;

public class CouponAdapter extends BaseAdapter implements View.OnClickListener {

    private List<CouponBean> objects;

    private Context context;
    private LayoutInflater layoutInflater;

    public CouponAdapter(Context context,List<CouponBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CouponBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_coupon, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((CouponBean)getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(CouponBean object, ViewHolder holder, int position) {
        //TODO implement
        holder.tvCouponPrice.setText(object.getPrice());
        holder.tvCouponReduce.setText(object.getReduce());
        holder.tvCouponResidue.setText(object.getResidue());
        holder.tvCouponTime.setText(object.getTime());
        holder.btnCouponAdd.setOnClickListener(this);
        holder.btnCouponDelete.setOnClickListener(this);
        holder.btnCouponDeleteLeft.setOnClickListener(this);
        holder.btnCouponDeleteRight.setOnClickListener(this);
        holder.btnCouponEdit.setOnClickListener(this);

        holder.btnCouponAdd.setTag(position);
        holder.btnCouponDelete.setTag(position);
        holder.btnCouponDeleteLeft.setTag(position);
        holder.btnCouponDeleteRight.setTag(position);
        holder.btnCouponEdit.setTag(position);

        switch (object.getTag()){
            case 0:
                holder.tagTwo.setVisibility(View.GONE);
                holder.tagThree.setVisibility(View.GONE);
                holder.tagOne.setVisibility(View.VISIBLE);
                break;
            case 1:
                holder.tagOne.setVisibility(View.GONE);
                holder.tagThree.setVisibility(View.GONE);
                holder.tagTwo.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.tagOne.setVisibility(View.GONE);
                holder.tagTwo.setVisibility(View.GONE);
                holder.tagThree.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public void onClick(View v) {
       int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.btn_coupon_delete_left:
                break;
            case R.id.btn_coupon_delete:
                break;
            case R.id.btn_coupon_delete_right:
                break;
            case R.id.btn_coupon_add:
                break;
            case R.id.btn_coupon_edit:
                break;


        }
    }

    protected class ViewHolder {
    private TextView tvCouponPrice;
    private TextView tvCouponReduce;
    private TextView tvCouponResidue;
    private TextView tvCouponTime;
        private LinearLayout tagOne;
        private Button btnCouponDeleteLeft;
        private Button btnCouponEdit;
        private LinearLayout tagTwo;
        private Button btnCouponDelete;
        private Button btnCouponAdd;
        private LinearLayout tagThree;
        private Button btnCouponDeleteRight;

        public ViewHolder(View view) {
            tvCouponPrice = (TextView) view.findViewById(R.id.tv_coupon_price);
            tvCouponReduce = (TextView) view.findViewById(R.id.tv_coupon_reduce);
            tvCouponResidue = (TextView) view.findViewById(R.id.tv_coupon_residue);
            tvCouponTime = (TextView) view.findViewById(R.id.tv_coupon_time);
            tagOne = (LinearLayout) view.findViewById(R.id.tag_one);
            btnCouponDeleteLeft = (Button) view.findViewById(R.id.btn_coupon_delete_left);
            btnCouponEdit = (Button) view.findViewById(R.id.btn_coupon_edit);
            tagTwo = (LinearLayout) view.findViewById(R.id.tag_two);
            btnCouponDelete = (Button) view.findViewById(R.id.btn_coupon_delete);
            btnCouponAdd = (Button) view.findViewById(R.id.btn_coupon_add);
            tagThree = (LinearLayout) view.findViewById(R.id.tag_three);
            btnCouponDeleteRight = (Button) view.findViewById(R.id.btn_coupon_delete_right);
        }
    }
}
